package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import classes_basicas.Cliente;
import classes_basicas.Endereco;
import fachada.Fachada;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Cad_Cliente {

	public JFrame frame;
	private JTextField txbnome;
	private JTextField txbcpf;
	private JTextField txbtelefone;
	private JTextField txblogradouro;
	private JTextField txbbairro;
	private JTextField txbcidade;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Cad_Cliente window = new Cad_Cliente();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Cad_Cliente() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 429, 476);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(128, 0, 0), 2));
		panel.setBackground(new Color(135, 206, 250));
		panel.setBounds(0, 0, 421, 445);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Cadastro de Clientes");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblNewLabel.setForeground(new Color(128, 0, 0));
		lblNewLabel.setBounds(118, 42, 196, 25);
		panel.add(lblNewLabel);
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(59, 107, 46, 14);
		panel.add(lblNome);
		
		JLabel lblCpf = new JLabel("Cpf:");
		lblCpf.setBounds(59, 132, 46, 14);
		panel.add(lblCpf);
		
		JLabel lblSexo = new JLabel("Sexo:");
		lblSexo.setBounds(59, 157, 46, 14);
		panel.add(lblSexo);
		
		JLabel lblTelefone = new JLabel("Telefone:");
		lblTelefone.setBounds(59, 183, 62, 14);
		panel.add(lblTelefone);
		
		txbnome = new JTextField();
		txbnome.setBounds(97, 104, 246, 20);
		panel.add(txbnome);
		txbnome.setColumns(10);
		
		txbcpf = new JTextField();
		txbcpf.setColumns(10);
		txbcpf.setBounds(97, 132, 246, 20);
		panel.add(txbcpf);
		
		txbtelefone = new JTextField();
		txbtelefone.setColumns(10);
		txbtelefone.setBounds(118, 180, 225, 20);
		panel.add(txbtelefone);
		
		final JComboBox combosexo = new JComboBox();
		combosexo.setModel(new DefaultComboBoxModel(new String[] {"Selecione o sexo", "Masculino", "Feminino"}));
		combosexo.setBounds(97, 157, 144, 20);
		panel.add(combosexo);
		
		JLabel lblLogradouro = new JLabel("Logradouro:");
		lblLogradouro.setBounds(59, 208, 73, 14);
		panel.add(lblLogradouro);
		
		txblogradouro = new JTextField();
		txblogradouro.setColumns(10);
		txblogradouro.setBounds(128, 205, 215, 20);
		panel.add(txblogradouro);
		
		JLabel lblBairro = new JLabel("Bairro:");
		lblBairro.setBounds(59, 234, 62, 14);
		panel.add(lblBairro);
		
		txbbairro = new JTextField();
		txbbairro.setColumns(10);
		txbbairro.setBounds(97, 231, 246, 20);
		panel.add(txbbairro);
		
		JLabel lblCidade = new JLabel("Cidade:");
		lblCidade.setBounds(59, 262, 62, 14);
		panel.add(lblCidade);
		
		txbcidade = new JTextField();
		txbcidade.setColumns(10);
		txbcidade.setBounds(118, 259, 225, 20);
		panel.add(txbcidade);
		
		JLabel lblEstado = new JLabel("Estado:");
		lblEstado.setBounds(59, 290, 62, 14);
		panel.add(lblEstado);
		
		final JComboBox comboestado = new JComboBox();
		comboestado.setModel(new DefaultComboBoxModel(new String[] {"Selecione o estado:", "Acre ", "Alagoas ", "Amap\u00E1 ", "Amazonas ", "Bahia  ", "Cear\u00E1 ", "Distrito Federal  ", "Esp\u00EDrito Santo ", "Goi\u00E1s", "Maranh\u00E3o ", "Mato Grosso ", "Mato Grosso do Sul ", "Minas Gerais ", "Par\u00E1 ", "Para\u00EDba ", "Paran\u00E1 ", "Pernambuco ", "Piau\u00ED ", "Rio de Janeiro ", "Rio Grande do Norte ", "Rio Grande do Sul ", "Rond\u00F4nia ", "Roraima ", "Santa Catarina ", "S\u00E3o Paulo ", "Sergipe ", "Tocantins "}));
		comboestado.setBounds(112, 287, 144, 20);
		panel.add(comboestado);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Cliente c = new Cliente();
				Endereco e = new Endereco();
				try {
					c.setNome(txbnome.getText());
					c.setCpf(txbcpf.getText());
					c.setSexo((String) combosexo.getSelectedItem());
					c.setTelefone(txbtelefone.getText());
					e.setLogradouro(txblogradouro.getText());
					e.setBairro(txbbairro.getText());
					e.setCidade(txbcidade.getText());
					e.setEstado((String) comboestado.getSelectedItem());
					c.setEndereco(e);
					
					Fachada f = new Fachada();
					
						f.inserircliente(c);
						JOptionPane.showMessageDialog(null,"Cadastrado com Sucesso!");
					
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,e1.getMessage());
				}
				
			}
		});
		btnSalvar.setBounds(122, 340, 90, 28);
		panel.add(btnSalvar);
		
		JButton btnVoltar = new JButton("Voltar");
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			Menu m = new Menu();
			frame.setVisible(false);
			m.frame.show();
			}
		});
		btnVoltar.setBounds(224, 340, 90, 28);
		panel.add(btnVoltar);
	}
}
